/*
 * Systemprogrammierung * Multiplayer-Quiz
 *
 * Client
 * 
 * listener.c: Implementierung des Listener-Threads
 */

#include "listener.h"

#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>

#include "../common/rfc.h"
#include "gui/gui_interface.h"
#include <semaphore.h>
#include <stdbool.h>

USER userlist[4];
int spielerzahl = 0;
sem_t frage;

playerlistEmpfangen(PACKET packet,bool gameRun) {
	printf("Test: %i\n", packet.header.length);
	spielerzahl = ntohs(packet.header.length) / 37;
	printf("%i Spieler in Playerlist\n", spielerzahl);
	preparation_clearPlayers();
	for (int i = 0; i < spielerzahl; i++) {
		userlist[i].id = packet.content.playerlist[i].id;
		strncpy(userlist[i].name, packet.content.playerlist[i].playername, 32);
		printf("test2\n");
		if(!gameRun){
		preparation_addPlayer(userlist[packet.content.playerlist[i].id].name);
		}else{
		game_setPlayerName( i+1,(userlist[packet.content.playerlist[i].id].name));
		}
		printf("test3\n");
	}
}

 /*void playerlistNeuanzeigen(PACKET packet) {
	printf("Test: %i\n", packet.header.length);
	spielerzahl = ntohs(packet.header.length) / 37;
	printf("%i Spieler in Playerlist\n", spielerzahl);
	preparation_clearPlayers();
	for (int i = 0; i < spielerzahl; i++) {
		userlist[i].id = packet.content.playerlist[i].id;
		strncpy(userlist[i].name, packet.content.playerlist[i].playername, 32);
		printf("test2\n");
		//game_setPlayerName(2,name )
		game_setPlayerName( i+1,(userlist[packet.content.playerlist[i].id].name));
		printf("test3\n");
	}
}*/





ErrorMessageEmpfangen(PACKET packet) {
	char message[(ntohs(packet.header.length))];
	strncpy(message, packet.content.error.errormessage,
			ntohs(packet.header.length) - 1);

	message[ntohs(packet.header.length) - 1] = '\0'; // Nullterminierung

	guiShowErrorDialog(message, packet.content.error.errortype);
	if (packet.content.error.errortype == 1) {
		exit(0);
	}
}
questionEmpfangen(PACKET packet){
	PACKET question;
	//question=question.content.question;
				/*if (ntohs(packet.header.length) > 0) {
									char buffer[ntohs(packet.header.length)];
									strncpy(buffer, packet.content.question,
											ntohs(packet.header.length));
									buffer[ntohs(packet.header.length)] = '\0';
									packet.header.length = htons(strlen(buffer));
									// strncpy(packet.content.question, buffer, strlen(buffer)); //wo ichs hol, dann wo ichs reinschreib
									// packetSchreiben(packet, socketFragen);
									// printf("startgameEmpfangen\n");

								}*/

						printf("Starte Question %s",packet.content.question);

}
/*
startgameEmpfangen(PACKET packet){
	if (ntohs(packet.header.length) > 0) {
						char buffer[ntohs(packet.header.length)];
						strncpy(buffer, packet.content.catalogname,
								ntohs(packet.header.length));
						buffer[ntohs(packet.header.length)] = '\0';
						packet.header.length = htons(strlen(buffer));
						 //strncpy(packet.content.catalogname, buffer, strlen(buffer)); //wo ichs hol, dann wo ichs reinschreib
						// packetSchreiben(packet, socketDeskriptor);
						 printf("startgameEmpfangen\n");

					}

			printf("Starte Katalog %s",packet.content.catalogname);




	 	 preparation_hideWindow();
		 game_setAnswerButtonsEnabled(0);
		 questionRequest();

		 game_setQuestion("question1");
		 game_showWindow();
		 game_setStatusText(packet.content.catalogname);


}*/

//Funktion um zu antworten
questionAnswered() { //noch nicht

}

void *listener_main(int * sockD) {


sem_init(&frage,0,0);

	char name[20];
	int stop = 0;
	bool gameRun=false;
	while (stop == 0) {
		int socketDeskriptor = &sockD;
		PACKET packet = packetLesen(*sockD);
		switch (packet.header.type) {

	//	case 0:guiShowErrorDialog("Verbindung zum Server verloren!",packet.content.error.errortype	);
		break;

		//case RFC_LOGINREQUEST:  loginRequest(name);  //1  gemacht!case 2 macht server RFC_LOGINRESPONSEOK
		break;

		//case RFC_CATALOGREQUEST:catalogRequest(name); //3  gemacht!
		break;
	case RFC_CATALOGRESPONSE:
		if (ntohs(packet.header.length) > 0) {
			char buffer[ntohs(packet.header.length)];
			strncpy(buffer, packet.content.catalogname,
					ntohs(packet.header.length));
			buffer[ntohs(packet.header.length)] = '\0';
			preparation_addCatalog(buffer);
		}
		; //4
		break;
	case RFC_CATALOGCHANGE:
		//preparation_onCatalogChanged(name); //5  in main
		//!!!!!! eigene funtkion um selection auf der gui zu setzen, schon virhanden, packet auseinandernehmenbla


		if (ntohs(packet.header.length) > 0) {
					char buffer[ntohs(packet.header.length)];
					strncpy(buffer, packet.content.catalogname,
							ntohs(packet.header.length));
					buffer[ntohs(packet.header.length)] = '\0';
					preparation_selectCatalog(buffer);
				}

		printf("Starte Katalog %s",packet.content.catalogname);



		break;

	case RFC_PLAYERLIST:

		playerlistEmpfangen(packet,gameRun); //6
		//catalogRequest();
		break;
	case RFC_STARTGAME:
		gameRun=true;
		//preparation_onStartClicked(name); //7 in main
		//eigenen ansatz bla in neuem Switch
		//startgameEmpfangen(packet);
		questionRequest(*sockD);

		printf("Spiel gestartet\n");
		//questionRequest();

		game_showWindow();
		//game_setPlayerName(2,name );

		break;
	case RFC_QUESTION:
		game_setQuestion(packet.content.question.question);
		for(int i = 0; i < 4; i++){
			game_setAnswer(i, packet.content.question.answers[i]);
		}
		// questionEmpfangen(name);    // 8    case 9 macht server
		break;

	case RFC_QUESTIONANSWERED:
		questionAnswered();    // 10
		break;

	case 255:
		ErrorMessageEmpfangen(packet);
		break;
	default:
		break;
		}

	}
	return 0;
}

/*
 #include "listener.h"

 #include <netinet/in.h>
 #include <stdlib.h>
 #include <string.h>

 #include "../common/rfc.h"
 #include "gui/gui_interface.h"
 #include "main.c"


 USER userlist[4];
 int spielerzahl=0;

 void playerlistEmpfangen(PACKET packet){
 printf("Test: %i\n",packet.header.length);
 spielerzahl = ntohs(packet.header.length)/37;
 printf("%i Spieler in Playerlist\n",spielerzahl);
 preparation_clearPlayers();
 for (int i =0; i< spielerzahl;i++){
 userlist[i].id=packet.content.playerlist[i].id;
 strncpy(userlist[i].name, packet.content.playerlist[i].playername,32);
 printf("test2\n");
 preparation_addPlayer(userlist[packet.content.playerlist[i].id].name);
 printf("test3\n");
 }
 }

 void ErrorMessageEmpfangen (PACKET packet){
 char message[(ntohs (packet.header.length))];
 strncpy (message, packet.content.error.errormessage, ntohs (packet.header.length)-1);

 message[ntohs (packet.header.length)-1]= '\0'; // Nullterminierung

 guiShowErrorDialog(message, packet.content.error.errortype);
 if (packet.content.error.errortype==1){
 exit(0);
 }
 }


 //Funktion um Katalog anzufordern

/*void catalogRequest(char* name){

 PACKET packet;
 packet.header.type = RFC_CATALOGREQUEST;
 packet.header.length = htons(strlen(name));
 strncpy(packet.content.catalogname, name, strlen(name)); //wo ichs hol, dann wo ichs reinschreib
 packetSchreiben(packet, socketDeskriptor);
 printf("catalogRequest ausführen\n");
 }*/

/*int catalog=0;
 char name[31];
 PACKET packet;
 catalogResponseEmpfangen(PACKET packet){

 //catalog =ntohs(packet.header.length);

 for (int i =0; i< catalog;i++){
 name[i]=packet.content.catalogname[i];
 printf("%i Kataloge\n",name);
 strncpy(name[i], packet.content.catalogname[i],31);
 preparation_addCatalog(name[packet.content.catalogname[i]]);

 }
 }
 */


 //Funktion um Katalog anzufordern
 /*catalogRequest(char * name){

 PACKET packet;
 packet.header.type = RFC_CATALOGREQUEST;
 packet.header.length = htons(strlen(name));
 strncpy(packet.content.catalogname, name, strlen(name)); //wo ichs hol, dann wo ichs reinschreib
 packetSchreiben(packet, socketDeskriptor);
 printf("catalogRequest ausführen\n");
 }


 int catalog=0;
 char name[31];
 //PACKET packet;
 catalogResponseEmpfangen(PACKET packet){

 //catalog =ntohs(packet.header.length);

 for (int i =0; i< catalog;i++){
 name[i]=packet.content.catalogname[i];
 printf("%i Kataloge\n",name);
 strncpy(name[i], packet.content.catalogname[i],31);
 preparation_addCatalog(name[packet.content.catalogname[i]]);

 }
 }


*/




 /*questionRequest(){
 PACKET packet;
 packet.header.type = RFC_QUESTIONREQUEST;
 packet.header.length = 0;
 send(socketDeskriptor, &head, sizeof(head),0);
 printf("%i sizeofHEADERQUESTIONREQUEST\n",packet.header.length);
 printf("Ouiz Anfrage ist raus!\n");



 }*/
//questionAnswered(){//noch nicht

//}
/*

 void *listener_main(int * sockD){

 char name[5];
 int stop = 0;
 while(stop==0){

 PACKET packet =packetLesen(*sockD);
 switch (packet.header.type){

 case RFC_LOGINREQUEST:  loginRequest(name);  //1  gemacht!
 break;
 case RFC_CATALOGREQUEST:// catalogRequest(); //3  gemacht!
 break;
 case RFC_CATALOGRESPONSE:catalogResponseEmpfangen(packet); // ein paket übergeben erst Proto
 break;
 case RFC_CATALOGCHANGE  : preparation_onCatalogChanged(name);//catalogChange();//catalogChange()empfangen//5 gemacht!
 break;
 case RFC_PLAYERLIST  :playerlistEmpfangen(packet);//6 gemacht!
 break;
 case RFC_STARTGAME :  preparation_onStartClicked(name);//startGame(); //7 gemacht!
 break;
 case RFC_QUESTIONREQUEST: // questionRequest();//8 gemacht!
 break;
 case RFC_QUESTIONANSWERED : //questionAnswered(); //10 noch nicht
 break;

 case RFC_ERRORWARNING:ErrorMessageEmpfangen (packet);
 break;
 default:
 break;
 }



 }
 return 0;
 }
 */

